package com.study.dao.impl;

import com.study.bean.Telephone_new;
import com.study.dao.Telephone_newDao;
import com.study.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/*增删改查
*
*  增删改属于 返回整型  --- 即返回的改变的行数
*  查询--- 返回 泛型类型
* */
public class Telephone_newDaoImpl implements Telephone_newDao {



    @Override
    public int add(Telephone_new t) throws SQLException {
        String addsql = "insert into `telephone_new` (`name`,`gender`,`age`,`tel`,`qq`,`addr`) values (?,?,?,?,?,?) ";
        Object[] para = {t.getName(),t.getGender(),t.getAge(),t.getTel(),t.getQq(),t.getAddr()};
        QueryRunner queryRunner = new QueryRunner();
        int update = queryRunner.update(addsql, para);        //sql语句 以及相关的参数


        return update;
    }

    @Override
    public int del(Integer id) throws SQLException {
        String delsql = "delete from `telephone_new`  where `id` = ?";
        Object[] para = {id};
        QueryRunner queryRunner = new QueryRunner();
        int del = queryRunner.update(delsql, para);
        return del;
    }


    @Override
    public int update(Telephone_new t) throws SQLException {
        String updatesql = "update  `telephone_new` set `name` = ?,`gender` = ?,`age`= ?,`tel`= ?,`qq`= ?,`addr` = ? where `id`=?";
        Object[] para = {t.getName(),t.getGender(),t.getAge(),t.getTel(),t.getQq(),t.getAddr(),t.getId()};
        QueryRunner queryRunner = new QueryRunner();
        int update = queryRunner.update(updatesql, para);


        return update;
    }

    @Override
    public List<Telephone_new> seletByName(String name) throws SQLException {
        String sql =  "select * from `telephone_new` where `name` = ?";
        Object[] para = {name};
        QueryRunner queryRunner = new QueryRunner();
        List<Telephone_new> query = queryRunner.query(JdbcUtil.getConnection(),sql, new BeanListHandler<Telephone_new>(Telephone_new.class), para);


        if (!query.isEmpty()){
            return query;
        }
        return null;
    }

    @Override
    public List<Telephone_new> selectAll() throws SQLException {
        String sql =  "select * from `telephone_new`";
        QueryRunner queryRunner = new QueryRunner();

        List<Telephone_new> querylist = queryRunner.query(JdbcUtil.getConnection(),sql, new BeanListHandler<Telephone_new>(Telephone_new.class));
        if (!querylist.isEmpty()){
            return querylist;
        }
        return null;
    }

    @Override
    public Telephone_new selectById(Integer id) throws SQLException {
        String sql =  "select * from `telephone_new` where `id` = ?";
        Object[] para = {id};
        QueryRunner queryRunner = new QueryRunner();
        Telephone_new t = queryRunner.query(JdbcUtil.getConnection(),sql, new BeanHandler<Telephone_new>(Telephone_new.class), para);


        if (t != null){
            return  t;
        }
        return null;
    }
}
